{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from itertools import chain\n",
    "\n",
    "from bqplot import OrdinalColorScale, Figure, Graph\n",
    "from ipywidgets import Layout"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# mobile patent suits - http://bl.ocks.org/mbostock/1153292\n",
    "suits = [\n",
    "    {'source': 'Microsoft', 'target': 'Amazon', 'type': 'licensing'},\n",
    "    {'source': 'Microsoft', 'target': 'HTC', 'type': 'licensing'},\n",
    "    {'source': 'Samsung', 'target': 'Apple', 'type': 'suit'},\n",
    "    {'source': 'Motorola', 'target': 'Apple', 'type': 'suit'},\n",
    "    {'source': 'Nokia', 'target': 'Apple', 'type': 'resolved'},\n",
    "    {'source': 'HTC', 'target': 'Apple', 'type': 'suit'},\n",
    "    {'source': 'Kodak', 'target': 'Apple', 'type': 'suit'},\n",
    "    {'source': 'Microsoft', 'target': 'Barnes & Noble', 'type': 'suit'},\n",
    "    {'source': 'Microsoft', 'target': 'Foxconn', 'type': 'suit'},\n",
    "    {'source': 'Oracle', 'target': 'Google', 'type': 'suit'},\n",
    "    {'source': 'Apple', 'target': 'HTC', 'type': 'suit'},\n",
    "    {'source': 'Microsoft', 'target': 'Inventec', 'type': 'suit'},\n",
    "    {'source': 'Samsung', 'target': 'Kodak', 'type': 'resolved'},\n",
    "    {'source': 'LG', 'target': 'Kodak', 'type': 'resolved'},\n",
    "    {'source': 'RIM', 'target': 'Kodak', 'type': 'suit'},\n",
    "    {'source': 'Sony', 'target': 'LG', 'type': 'suit'},\n",
    "    {'source': 'Kodak', 'target': 'LG', 'type': 'resolved'},\n",
    "    {'source': 'Apple', 'target': 'Nokia', 'type': 'resolved'},\n",
    "    {'source': 'Qualcomm', 'target': 'Nokia', 'type': 'resolved'},\n",
    "    {'source': 'Apple', 'target': 'Motorola', 'type': 'suit'},\n",
    "    {'source': 'Microsoft', 'target': 'Motorola', 'type': 'suit'},\n",
    "    {'source': 'Motorola', 'target': 'Microsoft', 'type': 'suit'},\n",
    "    {'source': 'Huawei', 'target': 'ZTE', 'type': 'suit'},\n",
    "    {'source': 'Ericsson', 'target': 'ZTE', 'type': 'suit'},\n",
    "    {'source': 'Kodak', 'target': 'Samsung', 'type': 'resolved'},\n",
    "    {'source': 'Apple', 'target': 'Samsung', 'type': 'suit'},\n",
    "    {'source': 'Kodak', 'target': 'RIM', 'type': 'suit'},\n",
    "    {'source': 'Nokia', 'target': 'Qualcomm', 'type': 'suit'},\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# transform data into nodes and links\n",
    "nodes = list(set(chain(*((suit['source'], suit['target']) for suit in suits))))\n",
    "\n",
    "# set custom node attrs\n",
    "node_data = [{'label': node, 'shape_attrs': {'r': 6}, 'label_display': 'outside'} for node in nodes]\n",
    "# for links, source and target should be indices into the nodes list\n",
    "nodes_index_map = {node: i for i, node in enumerate(nodes)}\n",
    "link_data = [{'source': nodes_index_map[s['source']], \n",
    "              'target': nodes_index_map[s['target']], \n",
    "              'value': s['type']} for s in suits]\n",
    "\n",
    "# encode suit type with link color\n",
    "link_color_scale = OrdinalColorScale(domain=['licensing', 'suit', 'resolved'], \n",
    "                                     colors=['limegreen', 'dodgerblue', 'orangered'])\n",
    "graph = Graph(node_data=node_data, link_data=link_data, link_type='arc', \n",
    "              scales={'link_color': link_color_scale}, colors=['gray'], \n",
    "              directed=True, link_distance=100, charge=-600)\n",
    "Figure(marks=[graph], layout=Layout(height='900px', width='1000px'), title='Mobile Patent Suits')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
